#define LGFX_USE_V1

#include "common.h"

//蓝牙键盘
BleKeyboard bleKeyboard("ESP32 Media Keybord", "Mendako Lab", 100);

LGFX tft;
CST816D touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT);

/*屏幕分辨率*/
static const uint32_t screenWidth = 240;
static const uint32_t screenHeight = 240;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[2][screenWidth * buf_size];

#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(lv_log_level_t level, const char *file, uint32_t line, const char *fn_name, const char *dsc) {
  Serial.printf("%s(%s)@%d->%s\r\n", file, fn_name, line, dsc);
  Serial.flush();
}
#endif

/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  if (tft.getStartCount() == 0) {
    tft.endWrite();
  }

  tft.pushImageDMA(area->x1, area->y1, area->x2 - area->x1 + 1, area->y2 - area->y1 + 1, (lgfx::swap565_t *)&color_p->full);

  lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
}

/*Read the touchpad*/
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {

  bool touched;
  uint8_t gesture;
  uint16_t touchX, touchY;

  touched = touch.getTouch(&touchX, &touchY, &gesture);

  if (!touched) {
    data->state = LV_INDEV_STATE_REL;
  } else {
    data->state = LV_INDEV_STATE_PR;

    /*Set the coordinates*/
    data->point.x = touchX;
    data->point.y = touchY;
  }
}



uint16_t lastVolume;

//蓝牙键盘控制
void adjustVolume(lv_event_t *e) {
  // 音量调节
  lv_obj_t *label = (lv_obj_t *)lv_event_get_user_data(e);  // 获取传进来的用户数据label
  lv_obj_t *arc = lv_event_get_target(e);                   // 获取触发的控件arc
  uint16_t value = lv_arc_get_value(arc);
  if (value == lastVolume) {
    return;
  }
  if (value > lastVolume) {
    bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
  }else {
    bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
  }
  lastVolume = value;
}

void playPreviousSong(lv_event_t *e) {
  // 上一首
  bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
}

void playOrPauseMusic(lv_event_t *e) {
  // 播放或暂停
  bleKeyboard.write(KEY_MEDIA_PLAY);
}

void playNextSong(lv_event_t *e) {
  // 下一首
  bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
}


void lockScreen(lv_event_t * e)
{
	// 锁定屏幕
  bleKeyboard.press(KEY_LEFT_CTRL);
  bleKeyboard.press(KEY_LEFT_GUI);
  bleKeyboard.press('q');
  delay(100);
  bleKeyboard.releaseAll();
}


void setup() {

  Serial.begin(115200); /* prepare for possible serial debug */
  Serial.println("Starting BLE work!");
  bleKeyboard.begin();

  tft.init();
  tft.initDMA();
  tft.startWrite();
  tft.setColor(0, 0, 0);

  lv_init();

#if LV_USE_LOG != 0
  lv_log_register_print_cb(my_print); /* register print function for debugging */
#endif

  lv_disp_draw_buf_init(&draw_buf, buf[0], buf[1], screenWidth * buf_size);

  /*Initialize the display*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  /*Change the following line to your display resolution*/
  disp_drv.hor_res = screenWidth;
  disp_drv.ver_res = screenHeight;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  lv_disp_drv_register(&disp_drv);

  /*Initialize the (dummy) input device driver*/
  if (is_touch == 1) {
    touch.begin();
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register(&indev_drv);
  }

#if 0
   /* Create simple label */
   lv_obj_t *label = lv_label_create( lv_scr_act() );
   lv_label_set_text( label, "Hello Arduino! (V8.0.X)" );
   lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 );
#else

  ui_init();  //

#endif
  Serial.println("Setup done");

  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);

  pinMode(0, INPUT);

  //蓝牙
}

// long currentTime = millis();

void loop() {

  lv_timer_handler(); /* let the GUI do its work */
  delay(5);
}
